package ita.class1.group2.supermarket.customer.controller;



import ita.class1.group2.supermarket.common.pojo.ResponseMessage;
import ita.class1.group2.supermarket.common.annotation.Autowired;
import ita.class1.group2.supermarket.common.annotation.Component;
import ita.class1.group2.supermarket.common.annotation.RequestMapping;
import ita.class1.group2.supermarket.common.pojo.Page;
import ita.class1.group2.supermarket.common.util.HttpUtil;
import ita.class1.group2.supermarket.common.util.JsonUtil;
import ita.class1.group2.supermarket.customer.dto.API;
import ita.class1.group2.supermarket.customer.dto.StoreDetailInfomation;
import ita.class1.group2.supermarket.customer.handler.DataHandler;
import ita.class1.group2.supermarket.customer.pojo.Store;
import ita.class1.group2.supermarket.customer.pojo.StoreInformation;
import ita.class1.group2.supermarket.customer.service.StoreInformationService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class StoreInformationController {

    @Autowired
    private StoreInformationService storeInformationService;

    @RequestMapping("/stores")
    public Page<StoreDetailInfomation> stores(String params) {
        Map jsonmap = new HashMap<>();
        try {
            jsonmap = JsonUtil.json2map(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int pageIndex  = (int) jsonmap.get("pageIndex");
        int pageCount  = (int) jsonmap.get("pageCount");
        String jsonsStore = HttpUtil.doGet(API.urlGetStore);
        System.out.println("jsonsStore:"+jsonsStore);
        if(jsonsStore!=null){
            List<Store> stores = null;
            try {
                Map map = JsonUtil.json2map(jsonsStore);
                String jsons = JsonUtil.obj2json(map.get("content"));
                stores = JsonUtil.json2list(jsons,Store.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            List<String> sidsStore = new ArrayList<String>();
            for (int j =0; j<stores.size(); j++) {
                sidsStore.add(stores.get(j).getId());
            }
            Page<StoreDetailInfomation> page = storeInformationService.findStoreInformationsBySids(sidsStore,pageIndex,pageCount,stores);
            return page;

        }else{
            return null;
        }

    }


    @RequestMapping("/store/{id}")
    public StoreDetailInfomation getStoreDetailInformation(Map<String,String> params) {
        String id  = (String) params.get("id");
        System.out.println("id:"+id);
        String json = HttpUtil.doPost(API.GET_STORE_BY_ID_URL,params);
        System.out.println("json:"+json);
        //json:{"content":{"id":"e86942ead2f94b2090b03893e9122fd7","name":"OKä¾¿åˆ©åº—","address":"å?—æ–¹è½¯ä»¶å›­A1æ¥¼ä¸‹","license":"1234567890X","status":0},"resultCode":200,"message":null}
        Store store = new Store();
        try {
            Map map = JsonUtil.json2map(json);
            String jsons = JsonUtil.obj2json(map.get("content"));
            store = JsonUtil.json2pojo(jsons,Store.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        StoreInformation storeInformation = storeInformationService.findStoreInformation(id);
        System.out.println("sid:"+storeInformation.getsId());
        StoreDetailInfomation storeDetailInfomation = DataHandler.packIntoDetailInfomation(store,storeInformation);
        return storeDetailInfomation;
    }


}
